//
//  ESCardLastedJokesView.m
//  EasyStyle
//
//  Created by FK on 16/5/9.
//  Copyright © 2016年 FK. All rights reserved.
//

#import "ESCardLastedJokesView.h"
#import "ESJokesNetResult.h"

@interface ESCardLastedJokesView ()

@property (nonatomic, assign)NSInteger arrIndex;
@property (nonatomic, strong)NSMutableArray *regularData;

@end

@implementation ESCardLastedJokesView

- (void)awakeFromNib
{
    self.backgroundColor = [UIColor colorWithHex:@"ebfafa"];
    self.layer.cornerRadius = 3.0;
    
    _arrIndex = 0;
    
    
    [_nextDataBtn addTarget:self action:@selector(changeToNextJokes:) forControlEvents:UIControlEventTouchUpInside];
    
    _jokesContentBtn.enabled = NO;
//    [_jokesContentBtn addTarget:self action:@selector(getMoreJokes) forControlEvents:UIControlEventTouchUpInside];
}


- (void)loadCardWithData:(NSArray *)resultLastedJokes
{
    ESJokesNetResult *result = [ESJokesNetResult new];
    _regularData = [result getLastedJokesData:resultLastedJokes];
    
    [self changeToNextJokes:nil];
    
}

- (void)changeToNextJokes:(UIButton *)sender
{
    if (_arrIndex == _regularData.count) {
        [self getMoreJokes];
        return;
    }
    if (_arrIndex == _regularData.count -1 ) {
        [_nextDataBtn setTitle:@"查看更多" forState:UIControlStateNormal];
    }
    LastedjokesItem *jokeItem = _regularData[_arrIndex];
    NSString *content = [self filtrateUselessWord:jokeItem.content];
    [_jokesContentBtn setTitle:content forState:UIControlStateNormal];
    [self refreshHeightWithContentLabelNewSize];
    _arrIndex++;
    [self refreshMainCardListHeigth];
}

- (NSString *)filtrateUselessWord:(NSString *)sourceString
{
    NSString *aString = @"&#160;";
//    NSString *bString = @"</em>";
    NSString *filtratedStr = [sourceString stringByReplacingOccurrencesOfString:aString withString:@""];
//    filtratedStr = [filtratedStr stringByReplacingOccurrencesOfString:bString withString:@""];
    
    return filtratedStr;
}


//- (void)changeToPreviousJokes:(UIButton *)sender
//{
////    if (_arrIndex == 0) {
////        _arrIndex = _regularData.count - 1;
////    }
//    
//    NSInteger absoluteIndex = labs(_arrIndex) % _regularData.count;
//    
//    LastedjokesItem *jokeItem = _regularData[absoluteIndex];
//    _jokesContent.text = jokeItem.content;
//    
//    [self refreshHeightWithContentLabelNewSize];
//    
//    _arrIndex--;
//    
//    [self refreshMainCardListHeigth];
//
//}



- (void)refreshHeightWithContentLabelNewSize
{
    NSStringDrawingOptions options =  NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
    
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    [style setLineBreakMode:NSLineBreakByCharWrapping];
    
    NSDictionary *attributes = @{ NSFontAttributeName : _jokesContentBtn.titleLabel.font, NSParagraphStyleAttributeName : style };
    
    CGRect contentLabelSize = [_jokesContentBtn.currentTitle boundingRectWithSize:CGSizeMake(ScreenWidth-20, CGFLOAT_MAX) options:options attributes:attributes context:nil];
    
    [self setHeight:contentLabelSize.size.height + _nextDataBtn.height + _cardName.height + 40];    // 13 是 控件间的纵向间隙和
}


- (void)getMoreJokes
{
    if (_blockGetMoreJokesClick) {
        _blockGetMoreJokesClick();
    }
}

- (void)refreshMainCardListHeigth
{
    if (_buttonClick) {
        NSLog(@"frame.size.height=== %f",self.frame.size.height);
        _buttonClick(self.frame.size.height);
    }
}

@end
